package com.kaishengit.action;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.kaishengit.pojo.City;
import com.kaishengit.pojo.Province;
import com.kaishengit.service.CityService;
import com.kaishengit.service.ProvinceService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class XmlAction extends ActionSupport {
	
	HttpServletRequest request = (HttpServletRequest)
			ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST); 
	HttpServletResponse response = (HttpServletResponse)
			ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
	Map<String,Object> session = ActionContext.getContext().getSession();
	private static final long serialVersionUID = 1L;
	

	private ProvinceService provinceService;
	private CityService cityService;
	@Autowired
	public void setCityService(CityService cityService) {
		this.cityService = cityService;
	}

	private List<Province> provices=new ArrayList<Province>();
	private List<City> cityList=new ArrayList<City>();
	
	
	public String execute() throws Exception{
		
		
		
		return SUCCESS;
		
	}
	@Action("readXml")
   public String xml() throws Exception{
	   provices= provinceService.findAll();
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<data>");
		
		for(Province pro : provices) {
			out.print("<provinces>");
			out.print("<provinceId>"+pro.getId()+"</provinceId>");
			out.print("<province>"+pro.getProvince()+"</province>");
			out.print("</provinces>");
		}
		
		out.print("</data>");
		return null;
	   
   }
	@Action("readCity")
	public String readCity() throws Exception{
		 String provinceId=request.getParameter("province");
		   
			provinceId=new String(provinceId.getBytes("ISO-8859-1"),"UTF-8");
			System.out.println(provinceId);
			 List<City> cityList=cityService.findByProvinceId(provinceId);
			
			for(City city : cityList) {
				System.out.println(city.getCity());
			}
			
			response.setContentType("text/xml;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			out.print("<data>");
			
			for(City city : cityList) {
				out.print("<citys>");
				out.print("<cityId>"+city.getId() +"</cityId>");
				out.print("<city>"+city.getCity()+"</city>");
				out.print("</citys>");
			}
			
			out.print("</data>");
	
		return null;
		
		
	}
	@Autowired
	public ProvinceService getProvinceService() {
		return provinceService;
	}

	public void setProvinceService(ProvinceService provinceService) {
		this.provinceService = provinceService;
	}
	
	
	public List<Province> getProvices() {
		return provices;
	}

	public void setProvices(List<Province> provices) {
		this.provices = provices;
	}

}
